package com.yidu.king.controller;

import com.yidu.king.domain.Person;
import com.yidu.king.service.PersonService;
import com.yidu.king.utils.BaseMessage;
import com.yidu.king.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>@Description: 人员</p>
 * <p>@Author yidu-liandyao</p>
 * <p>@Date: 2021-01-13</p>
 * <p>@Time: 9:07</p>
 */
@Controller
@RequestMapping("/person")
public class PersonController {

    @Resource
    PersonService personService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> selectAll(Person person){
        List<Person> list = personService.selectAll(person);
        int count = personService.selectCount(person);
        return Tools.getLayUiMap(list,count);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public BaseMessage delete(Integer id){
        return personService.deleteByPrimaryKey(id)>0?
                BaseMessage.succes():BaseMessage.fail();
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(Person person){
        int rows = 0 ;
        if(person.getId()==null){
            rows = personService.insertSelective(person);
        }else {
            rows = personService.updateByPrimaryKeySelective(person);
        }
        return  rows>0?BaseMessage.succes():BaseMessage.fail();
    }

    /**
     * 显示修改,上屏
     * @param id
     * @return
     */
    @RequestMapping("/showUpdate")
    @ResponseBody
    public BaseMessage showUpdate(Integer id){
        Person p = personService.selectByPrimaryKey(id);
        return BaseMessage.succes(p);
    }
}
